package ${domain.packageName};

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.annotation.JSONField;

import lombok.Data;
import net.newfirst.web.framework.utils.CollectionUtil;

@Data
public class BaseDO implements Serializable {

	/** 起始页 */
	private int pageNo = 1;

	/** 每页记录数 */
	private int pageSize=20;

	public Integer getStart() {
		if (pageNo < 0 || pageSize < 0) {
			return 0;
		} else {
			return ((pageNo - 1) * pageSize);
		}
	}
	
	private List<String> orders;
	
	public BaseDO pushOrder(String order){
		if(StringUtils.isBlank(order)) return this;
		order = order.trim();
		if(!order.contains(" asc") && !order.contains(" desc")){
			return this;
		}
		if(null == orders) {
			orders = new ArrayList<String>();
		}
		orders.add(order);
		return this;
	}

	@JSONField(serialize=false)
	public String getOrders() {
		if(CollectionUtils.isNotEmpty(this.orders)){
			return new StringBuffer("order by ").append(CollectionUtil.joinString(this.orders, ",")).toString();
		}
		return "order by id desc";
	}
}
